﻿using System;
using System.Collections.Generic;
using System.Text;
using DTO;
using DAO;

namespace BUS
{
    public class BC_LP_ChiTietBUS
    {
        public static bool LapBC(long mBC_LP, long mLP)
        {
            return BC_LP_ChiTietDAO.LapCTBC(mBC_LP, mLP);
        }

        public static bool Update(long mBC_LP, long mLP, double dT, double tL)
        {
            return BC_LP_ChiTietDAO.Update(mBC_LP, mLP, dT, tL);
        }

        public static List<BC_LP_ChiTietDTO> LayDSTheoMBC(long mBC_LP)
        {
            return BC_LP_ChiTietDAO.LayDSTheoMBC(mBC_LP);
        }

        public static void TinhDT(long mBC_LP, long tBC)
        {
            List<BC_LP_ChiTietDTO> L = BC_LP_ChiTietBUS.LayDSTheoMBC(mBC_LP);
            List<double> tDT = new List<double>();
            List<double> tL = new List<double>();


            for (int i = 0; i < L.Count; i++)
            {
                double dT = 0;
                List<HD_ChiTietDTO> HD = HD_ChiTietBUS.LayDSTheoMLP(L[i].MaLoaiPhong.MaLoaiPhong);

                for (int j = 0; j < HD.Count; j++)
                {
                    PhieuThueDTO PT = PhieuThueBUS.LayPTTheoMPT(HD[j].MaPhieuThue.MaPhieuThue);
                    long m = (long)PT.NgayThue.Month;
                    if (m == tBC)
                        dT = dT + HD[j].ThanhTien;
                }

                tDT.Add(dT);
            }

            double t = 0;

            for (int i = 0; i < tDT.Count; i++)
            {
                t = t + tDT[i];
            }

            for (int i = 0; i < tDT.Count; i++)
            {
                double temp = 0;

                temp = tDT[i] / t * 100;
                tL.Add(temp);
            }

            for (int i = 0; i < tDT.Count; i++)
            {
                BC_LP_ChiTietBUS.Update(mBC_LP, L[i].MaLoaiPhong.MaLoaiPhong, tDT[i], tL[i]);
            }
        }
    }
}
